package com.example.volunteer.controller;

import com.example.volunteer.model.Volunteer;
import com.example.volunteer.service.VolunteerService;
import com.example.volunteer.util.PageResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;



@RestController
@RequestMapping("/api/volunteers")
public class VolunteerController {

    private static final Logger log = LoggerFactory.getLogger(VolunteerController.class);
    @Autowired
    private VolunteerService volunteerService;

    @PostMapping("/register")
    public ResponseEntity<String> register(@RequestBody Volunteer volunteer) {
        volunteerService.register(volunteer);
        return ResponseEntity.ok("志愿者注册成功");
    }

    @PutMapping("/update")
    @PreAuthorize("hasAnyRole('ADMIN', 'VOLUNTEER')")
    public ResponseEntity<String> update(@RequestBody Volunteer volunteer) {
        volunteerService.update(volunteer);
        return ResponseEntity.ok("志愿者信息更新成功");
    }

    @GetMapping("/id")
    @PreAuthorize("hasRole('ADMIN')")
    public ResponseEntity<PageResult<Volunteer>> getByIdWithPage(
            @RequestParam String volunteerId,
            @RequestParam(defaultValue = "1")int pageNum,
            @RequestParam(defaultValue = "10")int pageSize) {
        return ResponseEntity.ok(volunteerService.findByIdWithPage(volunteerId, pageNum, pageSize));
    }

    @DeleteMapping("{volunteerId}")
    @PreAuthorize("hasRole('ADMIN')")
    public ResponseEntity<String> deleteVolunteer(@PathVariable String volunteerId) {
        volunteerService.delete(volunteerId);
        return ResponseEntity.ok("志愿者删除成功");
    }

    @GetMapping()
    @PreAuthorize("hasRole('ADMIN')")
    public ResponseEntity<PageResult<Volunteer>> getVolunteersWithPage(
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize) {
        return ResponseEntity.ok(volunteerService.findVolunteersWithPage(pageNum, pageSize));
    }

    @GetMapping("/name")
    @PreAuthorize("hasRole('ADMIN')")
    public ResponseEntity<PageResult<Volunteer>> getByNameWithPage(
            @RequestParam String name,
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize) {
        return ResponseEntity.ok(volunteerService.findByNameWithPage(name, pageNum, pageSize));
    }

    @GetMapping("/email")
    @PreAuthorize("hasRole('ADMIN')")
    public ResponseEntity<PageResult<Volunteer>> getByEmailWithPage(
            @RequestParam String email,
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize) {
        return ResponseEntity.ok(volunteerService.findByEmailWithPage(email, pageNum, pageSize));
    }

    @GetMapping("/contact")
    @PreAuthorize("hasRole('ADMIN')")
    public ResponseEntity<PageResult<Volunteer>> getByContactWithPage(
            @RequestParam String contact,
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize) {
        return ResponseEntity.ok(volunteerService.findByContactWithPage(contact, pageNum, pageSize));
    }
}
